home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / LOCCounter.lha / LOCCounter / src / PatternMatch.cpp < prev    next >
C/C++ Source or Header  |  2004-08-17  |  3KB  |  116 lines

  1. /****************************************************************************
  2. *
  3. * $RCSfile: PatternMatch.cpp $
  4. * $Revision: 2.6 $
  5. * $Date: 2004/08/17 22:38:58 $
  6. * $Author: ssolie $
  7. *
  8. *****************************************************************************
  9. *
  10. * Copyright (c) 2004 Steven Solie.  All Rights Reserved.
  11. *
  12. *****************************************************************************
  13. *
  14. * PatternMatch component
  15. *
  16. * The PatternMatch class is used to match AmigaOS patterns.
  17. */
  18. #include "PatternMatch.h"
  19.  
  20. #include <dos/anchorpath.h>
  21. #include <dos/dos.h>
  22. #include <dos/dosasl.h>
  23. #include <exec/memory.h>
  24.  
  25. #include <proto/dos.h>
  26.  
  27. #include <new>
  28.  
  29.  
  30. PatternMatch::PatternMatch() :
  31.     m_anchor(0),
  32.     m_lock(0),
  33.     m_searching(false)
  34. {
  35.     m_anchor = reinterpret_cast<AnchorPath*>(IDOS->AllocDosObjectTags(
  36.         DOS_ANCHORPATH,
  37.         ADO_Strlen, 0,
  38.         ADO_Flags, 0,
  39.         ADO_Mask, SIGBREAKF_CTRL_C,
  40.         TAG_END));
  41.     if ( m_anchor == 0 )  {
  42.         throw std::bad_alloc();
  43.     }
  44. }
  45.  
  46.  
  47. PatternMatch::~PatternMatch()
  48. {
  49.     if ( m_searching )  {
  50.         IDOS->CurrentDir(m_lock);   // restore lock
  51.         IDOS->MatchEnd(m_anchor);
  52.     }
  53.  
  54.     IDOS->FreeDosObject(DOS_ANCHORPATH, m_anchor);
  55. }
  56.  
  57.  
  58. const char* PatternMatch::firstFile(const char* pattern)
  59. {
  60.     if ( m_searching )  {
  61.         IDOS->SetIoErr(ERROR_OBJECT_NOT_FOUND);
  62.         throw dos_error();
  63.     }
  64.  
  65.     int32 match = IDOS->MatchFirst(const_cast<STRPTR>(pattern), m_anchor);
  66.     while ( match == 0 && m_anchor->ap_Info.fib_DirEntryType > 0 )  {
  67.         m_anchor->ap_Flags &= ~APF_DIDDIR;
  68.         match = IDOS->MatchNext(m_anchor);
  69.     }
  70.  
  71.     if ( match != 0 )  {
  72.         IDOS->SetIoErr(match);
  73.         IDOS->MatchEnd(m_anchor);
  74.         throw dos_error();
  75.     }
  76.     else  {
  77.         m_searching = true;
  78.         m_lock = IDOS->CurrentDir(m_anchor->ap_Current->an_Lock);
  79.         return m_anchor->ap_Info.fib_FileName;
  80.     }
  81. }
  82.  
  83.  
  84. const char* PatternMatch::nextFile()
  85. {
  86.     if ( !m_searching )  {
  87.         IDOS->SetIoErr(ERROR_OBJECT_NOT_FOUND);
  88.         throw dos_error();
  89.     }
  90.  
  91.     IDOS->CurrentDir(m_lock);   // restore lock
  92.  
  93.     int32 match = IDOS->MatchNext(m_anchor);
  94.     while ( match == 0 && m_anchor->ap_Info.fib_DirEntryType > 0 )  {
  95.         m_anchor->ap_Flags &= ~APF_DIDDIR;
  96.         match = IDOS->MatchNext(m_anchor);
  97.     }
  98.  
  99.     if ( match == 0 )  {
  100.         m_lock = IDOS->CurrentDir(m_anchor->ap_Current->an_Lock);
  101.         return m_anchor->ap_Info.fib_FileName;
  102.     }
  103.     else if ( match == ERROR_NO_MORE_ENTRIES )  {
  104.         IDOS->MatchEnd(m_anchor);
  105.         m_searching = false;
  106.         return 0;
  107.     }
  108.     else  {
  109.         IDOS->SetIoErr(match);
  110.         IDOS->MatchEnd(m_anchor);
  111.         m_searching = false;
  112.         throw dos_error();
  113.     }
  114. }
  115.  
  116.